package cz.data.domain.market.mapping.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.market.mapping.mapstruct.ApiLogMapper;
import cz.data.domain.market.mapping.service.ApiLogService;
import cz.data.domain.market.model.dto.ApiLogDto;
import cz.data.domain.market.model.entity.ApiLogEntity;
import cz.data.domain.market.model.query.ApiLogQuery;
import cz.data.domain.market.model.vo.ApiLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"API调用日志"})
@RestController
public class ApiLogController extends BaseController {

    @Autowired
    private ApiLogService apiLogService;

    @Autowired
    private ApiLogMapper apiLogMapper;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "API调用日志-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/apiLogs/{id}")
    public SimpleDataResponse<ApiLogVo> getApiLogById(@PathVariable String id) {
        ApiLogEntity apiLogEntity = apiLogService.getApiLogById(id);
        return new SimpleDataResponse<>(apiLogMapper.toVO(apiLogEntity));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "API调用日志-分页查询")
    @ApiImplicitParam(name = "apiLogQuery", value = "查询实体apiLogQuery", required = true, dataTypeClass = ApiLogQuery.class)
    @GetMapping("/apiLogs/page")
    public SimpleDataResponse<JsonPage<ApiLogVo>> getApiLogPage(ApiLogQuery apiLogQuery) {
        QueryWrapper<ApiLogEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotBlank(apiLogQuery.getApiName()), "api.api_name", apiLogQuery.getApiName())
                .or()
                .like(StringUtils.isNotBlank(apiLogQuery.getApiName()), "tapi.api_name", apiLogQuery.getApiName());
        queryWrapper.lambda().orderByDesc(ApiLogEntity::getCallerDate);
        IPage<ApiLogEntity> page = apiLogService.page(new Page<>(apiLogQuery.getPageNum(), apiLogQuery.getPageSize()), queryWrapper);
        List<ApiLogVo> collect = page.getRecords().stream().map(apiLogMapper::toVO).collect(Collectors.toList());
        JsonPage<ApiLogVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "API调用日志-添加")
    @ApiImplicitParam(name = "apiLog", value = "详细实体apiLog", required = true, dataType = "ApiLogDto")
    @PostMapping("/apiLogs")
    public SimpleDataResponse<ApiLogVo> saveApiLog(@RequestBody @Validated({ValidationGroups.Insert.class}) ApiLogDto apiLog) {
        ApiLogEntity apiLogEntity = apiLogService.saveApiLog(apiLog);
        return new SimpleDataResponse<>(apiLogMapper.toVO(apiLogEntity));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "API调用日志-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "apiLog", value = "详细实体apiLog", required = true, dataType = "ApiLogDto")
    })
    @PutMapping("/apiLogs/{id}")
    public SimpleDataResponse<ApiLogVo> updateApiLog(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) ApiLogDto apiLog) {
        ApiLogEntity apiLogEntity = apiLogService.updateApiLog(apiLog);
        return new SimpleDataResponse<>(apiLogMapper.toVO(apiLogEntity));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "API调用日志-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/apiLogs/{id}")
    public BaseResponse deleteApiLogById(@PathVariable String id) {
        apiLogService.deleteApiLogById(id);
        return BaseResponse.success();
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "API调用日志-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/apiLogs/batch/{ids}")
    public BaseResponse deleteApiLogBatch(@PathVariable List<String> ids) {
        apiLogService.deleteApiLogBatch(ids);
        return BaseResponse.success();
    }
}
